<?php
namespace Admin\Controller;

use Common\Controller\AdminBaseController;

class ArticleController extends AdminBaseController{
    public function index(){
        $title = I('get.title/s','');
        $page = I('get.page/d',1);
        $article = M('article');
        //搜索功能
        $where = empty($title) ? '' : 'title like"%' . $title .'%" or content like"%'. $title .'%"';
        //分页
        $pageNum = 5;//每页5条记录
        $count = $article->where($where)->count(); // 查询满足要求的总记录数
        $pages = ceil($count / $pageNum);
        //获取当前的第一页
        $firstRow = empty($page - 1) ? 0 : ($page - 1) * $pages;
        //让搜索结果按降序显示
        $list = $article->Alias('a')->field('a.id,a.cat_id,a.author,a.hits,a.content,a.title,a.tag,a.status,a.sort,a.litpic,a.addtime,a.mid,ad.username')->join('left join dx_admin ad on ad.id = a.mid')->where($where)->order('a.id desc')->limit($firstRow,$pageNum)->select();
        $this -> assign('title', $title);
        $this -> assign('list', $list);
        $this -> assign('pages', $pages);
        $this -> display();
    }
    //添加文章
    public function add(){
        $list = M('columns')->field('id,pid,columns,status')->where(['type' => 1])->select();
        $list = getTree($list,0,0,'——');
        $mid = session('user.uid');
        $this->assign('mid',$mid);
        $this->assign('list',$list);
        $this -> display();
    }
    //修改文章
    public function edit(){
        $id = I('get.id/d',0);
        $list = M('columns')->field('id,columns')->select();
        $data = M('article')->field('id,cat_id,title,author,litpic,tag,seo_title,seo_description,seo_keyword,sort,status,addtime,content')->where('id='.$id)->find();
        $list = getTree($list,0,0,'——');
        $this->assign('data',$data);
        $this->assign('list',$list);
        $this->display('article/add');

    }

    //添加、修改文章处理
    public function save(){
        if(IS_POST || IS_AJAX){
            $data['id'] = I('post.id/d',0);
            $data['title'] = I('post.title/s','');
            if(empty($data['title'])){
                $this->ajaxReturn(['status' => 1,'标题不能为空！']);
            }
            $data['author'] = I('post.author/s','');
            if(empty($data['author'])){
                $this->ajaxReturn(['status' => 1,'作者不能为空！']);
            }
            $data['mid'] = I('post.mid/s','');
            $data['content'] = I('post.content/s','');
            if(empty($data['content'])){
                $this->ajaxReturn(['status' => 1,'内容不能为空！']);
            }
            $data['tag'] = I('post.tag/s','');
            if(empty($data['tag'])){
                $this->ajaxReturn(['status' => 1,'关键词不能为空！']);
            }
            $data['seo_title'] = I('post.seo_title/s','');
            if(empty($data['seo_title'])){
                $this->ajaxReturn(['status' => 1,'SEO标题不能为空！']);
            }
            $data['seo_keyword'] = I('post.seo_keyword/s','');
            if(empty($data['seo_keyword'])){
                $this->ajaxReturn(['status' => 1,'SEO关键词不能为空！']);
            }
            $data['seo_description'] = I('post.seo_description/s','');
            if(empty($data['seo_description'])){
                $this->ajaxReturn(['status' => 1,'SEO描述不能为空！']);
            }
            $data['sort'] = I('post.sort/s','');
            if(empty($data['sort'])){
                $this->ajaxReturn(['status' => 1,'排序不能为空！']);
            }
            $data['status'] = I('post.status/d',1);
            $data['cat_id'] = I('post.cat_id/s','');
            $data['is_html'] = I('post.is_html/d',1);
            $data['addtime'] = strtotime(I('post.addtime/s',''));
            //hits 点击文章，会调用显示文章的方法，这个方法里同时写一个增加点击次数的逻辑，在文章表里增加一个点击数字段，每次点击文章，这个字段加1

            //上传缩略图
            $data['litpic'] = '';
            if(!empty($_FILES)){
                $data['litpic'] = '/' . uploadFile($_FILES['litpic']);
            }
            if(is_array($data['litpic'])){
                $this->ajaxReturn($data['litpic']);
            }
            $article = M('article');
            $is_Article = $article->where(['title' => $data['title'],'cat_id' => $data['cat_id']])->find();
            if($is_Article){
                $this->ajaxReturn(['status' => 1,'msg' => '该文章已存在！']);
            }          
            $result = $article->add($data,[],true);
            if(!$result){
                $this->ajaxReturn(['status' => 1,'msg' => '失败']);
            }else{
                $this->ajaxReturn(['status' => 0,'msg' => '成功']);
            }
        }
    }
   
    /**
     * 修改状态
     */
    public function changeState(){
        if(IS_POST){
            $id = I('post.id/d',0);
            $article = M('article');
            $articleInfo = $article -> field('status') -> where(['id' => $id]) -> find();
            if(empty($articleInfo['status'])){
                $article -> where(['id' => $id]) -> setField('status',1);
                $this->ajaxReturn(['status' => 0 , 'msg' => '已开启' , 'data' => 1]);
            }else{
                $article -> where(['id' => $id]) -> setField('status',0);
            }
            $this->ajaxReturn(['status' => 0 , 'msg' => '已禁用', 'data' => 0]);
        }else{
            $this->ajaxReturn(['status' => 1 , 'msg' => '非法操作！']);
        }
    }
    /**
     * 删除
     */
    public function del(){
        if(IS_POST){
            $id  = I('post.id/d',0);
            $article = M('article');
            $isDel = $article -> where(['id' => $id]) -> delete();
            if(empty($isDel)){
                $this->ajaxReturn(['status' => 1 , 'msg' => '删除失败！']);
            }
            $this->ajaxReturn(['status' => 0 , 'msg' => '删除成功！']);
        }else{
            $this->ajaxReturn(['status' => 1 , 'msg' => '非法操作！']);
        }
    }
            
}